import {SET_REDBAG} from "../constants/redux"
import {http} from "../utils/http"
import {GET_REDBAG_SET} from "../constants/api"
import {RedBagSetting} from "../utils/server.interface"
import {user} from "../utils/user.service";

interface RedBag {
  isShow: boolean
  image: string
}

const onSetRedBag = (redbag: RedBag) => {
  return {
    type: SET_REDBAG,
    redbag
  }
}

export function getRedBagSet() {
  return async (dispatch) => {
    if (await user.checkLogin() && process.env.IS_SHOW_RED_BAG) {
      const setting: any = await http.get<RedBagSetting>(GET_REDBAG_SET, {})
      if (setting) {
        dispatch(onSetRedBag({isShow: Boolean(setting.moduleSet.isHongbao), image: setting.moduleSet.hongbaoImg}))
      } else {
        dispatch(onSetRedBag({isShow: false, image: ''}))
      }
    }
  }
}
